//
//  MBSubdaryCell.m
//  发布求租
//
//  Created by MB__Lee on 2018/6/9.
//  Copyright © 2018年 MB__Lee. All rights reserved.
//

#import "MBSubdaryCell.h"
#import "MBCircleBTN.h"

@interface MBSubdaryCell()
@property (weak, nonatomic) IBOutlet UILabel *peiTaoLabel;


@property(nonatomic,strong)NSMutableArray *tagMut; //用来记录button 的tag值
@end

@implementation MBSubdaryCell
-(NSMutableArray *)tagMut{
    if (!_tagMut) {
        _tagMut = [NSMutableArray array];
    }
    return  _tagMut;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
}
-(void)setBTNModel:(MBCircleBTNModel *)BTNModel{
    [super setBTNModel:BTNModel];
    
    self.peiTaoLabel.text = BTNModel.title;
//    // *  是否隐藏
//    if (BTNModel.hasX) {
//        self.xingLabel.hidden = NO;
//    }else{
//        self.xingLabel.hidden = YES;
//    }
    
    // 2.生成按钮
    CGFloat margin = 10;
    
    CGFloat hight = 32;
    
    MBCircleBTN *tempBtn = [[MBCircleBTN alloc] initWithFrame:CGRectMake(0, 50, 0, 0)];
    
    for (int i = 0; i< BTNModel.btnNameMut.count; i++) {
        //计算frame
        CGFloat x = CGRectGetMaxX(tempBtn.frame);
        x += margin;
        
        CGFloat width = [self caculateStringWidth:BTNModel.btnNameMut[i]];
        
        CGFloat allwith = x + width;
        
        CGFloat y = tempBtn.frame.origin.y;
        
        if (allwith + margin > SCREEN_WIDTH) {
            x = margin;
            
            y =  CGRectGetMaxY(tempBtn.frame)+ margin;
        }
        
        
        MBCircleBTN *btn = [MBCircleBTN buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(x, y, width, hight);
        [btn setTitle:BTNModel.btnNameMut[i] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(backBTN:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.layer.cornerRadius = 0.5 *hight;
        
        [self addSubview:btn];
        
        btn.tag = 100+ i;
        
        tempBtn = btn;
        
        
    }
    
}
-(CGFloat)caculateStringWidth:(NSString *)str{
    NSDictionary *attrs = @{NSFontAttributeName : MBFont(14)};
    CGSize maxSize = CGSizeMake(CGFLOAT_MAX, 32);
    return [str boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width+ 40;
}
-(void)backBTN:(MBCircleBTN *)btn{
    btn.selected = !btn.selected;
    
  
    NSString *selectedString = self.BTNModel.btnNameMut[btn.tag - 100];
    
    if ([self.tagMut containsObject:selectedString]) {
        [self.tagMut removeObject:selectedString];
    }else{
        [self.tagMut addObject:selectedString];
    }
    
    self.BTNModel.subMut = self.tagMut;
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
